<template>
  <border
    class="operating-income-of-the-park"
    :moduleType="borderConfig.moduleType"
    :icon="borderConfig.icon"
    :titleText="borderConfig.titleText"
    :goPageUrl="borderConfig.goPageUrl">
    <div class="module-content" slot="module-content">
      <div class="content-left">
        <message :messageImgSrc="message[0].messageImgSrc"
                 :messageNumber="message[0].messageNumber"
                 :messageName="message[0].messageName"></message>
        <pie :ref="pieConfig.ref[0]"
             :isUseValue="pieConfig.isUseValue"
             :value="pieConfig.pieData[0]"
             :pieLegend="pieConfig.legend[0]"
             :limitLength="pieConfig.limitLength"
             :pieType="pieType"
             :notOther="pieConfig.notOther"
             :labelShow="pieConfig.labelShow"
             :seriesRadius="seriesRadius"
             :dtottedLine="pieConfig.dtottedLine"
             :isCanEmpty="pieConfig.isCanEmpty"
             :tooltipsUnit="pieConfig.pieUnit[0]"
             :isUseComputedLine="pieConfig.isUseComputedLine"></pie>
      </div>
      <div class="content-right">
        <message :messageImgSrc="message[1].messageImgSrc"
                 :messageNumber="message[1].messageNumber"
                 :messageName="message[1].messageName"></message>
        <pie :ref="pieConfig.ref[1]"
             :isUseValue="pieConfig.isUseValue"
             :value="pieConfig.pieData[1]"
             :pieLegend="pieConfig.legend[1]"
             :limitLength="pieConfig.limitLength"
             :pieType="pieType"
             :notOther="pieConfig.notOther"
             :labelShow="pieConfig.labelShow"
             :seriesRadius="seriesRadius"
             :isCanEmpty="pieConfig.isCanEmpty"
             :dtottedLine="pieConfig.dtottedLine"
             :tooltipsUnit="pieConfig.pieUnit[1]"
             :isUseComputedLine="pieConfig.isUseComputedLine"></pie>
      </div>
      <div class="border-pic"></div>
      <div class="legend">
        <div class="legend-li" v-for="(item, index) in pieConfig.legend[0]" :key="index" :class="{'hide-legend-li': index > 5}">
          <div class="legend-li-content"  v-if="index < pieConfig.limitLength - 1">
            <i :style="{'background': pieConfig.pieColor[index]}"></i>
            <span>{{ item.length > 4 ? item.substring(0, 4) : item }}</span>
          </div>
          <div class="legend-li-content" v-else-if="index == pieConfig.limitLength - 1">
            <i :style="{'background': pieConfig.pieColor[index]}"></i>
            <span>{{ pieConfig.legend.length === 5 ?item.length > 4 ? item.substring(0, 4) : item : '其他' }}</span>
          </div>
        </div>
      </div>
    </div>
  </border>
</template>

<script>
import Border from './../components/Border.vue'
import Pie from './../components/Pie.vue'
import Message from './../components/Message.vue'
export default {
  name: 'operatingIncomeOfThePark',
  components: {
    Border,
    Pie,
    Message
  },
  data () {
    return {
      borderConfig: {
        moduleType: '1x1',
        icon: '',
        titleText: '园区经营收入',
        goPageUrl: ''
      },
      message: [
        {
          messageName: '本年累计收入（万元）',
          messageNumber: 0,
          messageImgSrc: require('./../../assets/images/module/background/octagon230x87.png')
        },
        {
          messageName: '本月累计收入（万元）',
          messageNumber: 0,
          messageImgSrc: require('./../../assets/images/module/background/octagon230x87.png')
        }
      ],
      pieConfig: {
        legend: [[], []],
        pieColor: ['#e9e703', '#e8605e', '#19dc7c', '#319ced', '#fa9022', '#8c70f8', '#cadfea', '#f59233'],
        pieData: [[], []],
        limitLength: 5,
        notOther: 'notOther',
        isUseValue: true,
        labelShow: false,
        isUseComputedLine: true,
        dtottedLine: false,
        isCanEmpty: false,
        ref: ['ref1', 'ref2'],
        title: ['服务项目分布', '订单分布'],
        pieUnit: ['万元', '万元']
      },
      apiData: {},
      refreshInterval: 0
    }
  },
  computed: {
    pieType () {
      return this.$store.state.themeConfig.echartsTheme === 'default' ? '' : 'default'
    },
    seriesRadius () {
      return this.$store.state.themeConfig.echartsTheme === 'default' ? [['15%', '50%'], ['5%', '10%'], ['55%', '56%'], ['62%', '63%'], ['69%', '70%']] : [['40%', '70%']]
    },
    organId () {
      return this.$store.state.organId
    },
    isIndex () {
      return this.$store.state.isIndex
    }
  },
  methods: {
    showTips  (data) {
      this.$parent.showTips(data)
    },
    getData (type, refreshInterval, isRefresh) {
      if (type === 'init') {
        this.operationApi.businessResourceFee({ organId: this.organId }).then(res => {
          if (res.data.code === '0') {
            let data = res.data.data
            this.apiData = data
            this.message[0].messageNumber = parseInt(data.yearFee / 1000000)
            this.message[1].messageNumber = parseInt(data.monthFee / 1000000)
            if (!isRefresh) {
              this.handleData()
            }
          } else {
            this.apiData = {}
            this.$parent.showTips('请求失败，点击确认继续刷新')
          }
        }, res => {
          this.apiData = {}
          this.topMessage.message = [0, 0, 0, 0, 0]
          this.$parent.showTips('请求失败，点击确认继续刷新')
        })
        if (refreshInterval) {
          this.refreshInterval = refreshInterval
          this.automaticRedrawing()
        }
      } else {
        this.handleData()
      }
    },
    handleData () {
      let data = this.apiData
      let legend = []
      let legend1 = []
      let pieData1 = []
      let pieData2 = []
      if (JSON.stringify(data) !== '{}') {
        data.yearFeeDistributed.forEach((item, index) => {
          legend.push(item.name)
          pieData1.push(parseInt(item.value / 1000000))
          if (data.monthFeeDistributed[index]) {
            legend1.push(data.monthFeeDistributed[index].name)
            pieData2.push(parseInt(parseInt(data.monthFeeDistributed[index].value / 1000000)))
          }
        })
      }
      this.pieConfig.legend = [legend, legend1]
      this.pieConfig.pieData = [pieData1, pieData2]
    },
    resizeChart () {
      this.$refs.ref1[0].resizeChart()
      this.$refs.ref2[0].resizeChart()
    },
    // 根据接口返回的时间间隔请求接口
    automaticRedrawing () {
      clearInterval(window.operatingIncomeOfTheParkTimer)
      if (this.refreshInterval > 0 && this.isIndex) {
        window.operatingIncomeOfTheParkTimer = setInterval(() => {
          this.getData('init', '', true)
        }, this.refreshInterval * 1000)
      }
    }
  },
  mounted () {
  },
  destroyed () {
    clearInterval(window.operatingIncomeOfTheParkTimer)
  }
}
</script>
<style lang="scss" scoped>
  .operating-income-of-the-park {
    .module-content {
      width: 100%;
      height: 100%;
      .content-left, .content-right {
        width: 50%;
        height: 80%;
        float: left;
        .message {
          width: calc(100% - 52px);
          margin: 10px 0 0 26px;
        }
        .pie {
          width: 100%;
          height: calc(70% - 10px);
          margin-top: 10px;
        }
      }
      .border-pic {
        width: 1px;
        height: 70%;
        position: absolute;
        top: 10%;
        left: 50%;
        transform: translate(-50%, 0);
        background: url("./../../assets/images/module/pic-line-vertical.png") no-repeat center;
        background-size: 100% 100%;
      }
      .legend {
        width: calc(100% - 40px);
        height: 20%;
        padding: 0 20px;
        display: flex;
        align-items: center;
        align-content: center;
        justify-items: center;
        justify-content: center;
        flex-wrap: wrap;
        float: left;
        .legend-li {
          margin-right: 30px;
          line-height: 24px;
          &.hide-legend-li {
            display: none;
          }
          &:last-of-type {
            margin: 0;
          }
          i {
            display: block;
            float: left;
            width: 12px;
            height: 12px;
            vertical-align: top;
            margin: 1px 5px 0 0;
          }
          span {
            font-size: 12px;
            color: #A4B3C7;
            line-height: 14px;
            display: inline-block;
            vertical-align: top;
          }
        }
      }
    }
  }
  @media screen and (min-width: 1890px) {
    .operating-income-of-the-park {
      .module-content {

      }
    }
  }
</style>
